package jsyf.oa.dao.capital;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import jsyf.oa.entity.capital.BankDebitsEntity;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

import jsyf.oa.idao.BaseDao;
import jsyf.oa.ientity.IBaseEntity;

/**
 * Created by houtailei on 2018/2/25.
 */
@Repository
public class BankDebitsDao extends BaseDao {

    public BankDebitsDao() {
        super.iBaseEntity = new BankDebitsEntity();
    }

    public List<BankDebitsEntity> getEntityListByCondition(String condition, Object[] params) {
        List<JSONObject> jsonList = getJSONListByCondition(condition, params);
        List<BankDebitsEntity> entityList = new ArrayList<>();
        for (int i = 0; i < jsonList.size(); i++) {
            BankDebitsEntity entity = (BankDebitsEntity) convertJson2Entity(jsonList.get(i));
            entityList.add(entity);
        }
        return entityList;
    }

    @Override
    public IBaseEntity convertJson2Entity(JSONObject json) {
        return JSON.toJavaObject(json, BankDebitsEntity.class);
    }

    public List<JSONObject> getBankDebitsByInput(String dateTime, String companyId) {
        StringBuffer sb = new StringBuffer();
        sb.append(" SELECT ");
        sb.append(" t_cpt_bank_debits.ID as id, \n");
        sb.append(" t_cpt_bank_debits.MONEY as money, \n");
        sb.append(" t_cpt_bank_debits.INCOME_PROJECT AS incomeProject, \n");
        sb.append(" t_cpt_bank_debits.DUE_DATE AS dueDate, \n");
        sb.append(" t_cpt_bank_debits.DRAWER_BANK AS drawerBank,\n");
        sb.append(" t_cpt_bank_debits.DEBITS_NO AS debitsNo \n");
        sb.append(" FROM `t_cpt_bank_debits`  \n");
        sb.append(" where 1=1 \n");
        sb.append(" AND FK_COMPANY_ID = " + "'" + companyId + "' \n");
        sb.append(" AND (IMPAWN_FLAG=0 \n");
        sb.append(" AND DEL_FLAG=0 ");
        sb.append(" AND (ENDORSE_FLAG=0  AND LEFT(CREATE_TIME,10) <=" + "'" + dateTime + "') \n");
        sb.append(" OR (ENDORSE_FLAG=1 AND ENDORSE_MODE=0 AND COLLECTION_ARRIVE_FLAG=0 \n");
        sb.append(" AND (COLLECTION_ARRIVE_DATE<=" + "'" + dateTime + "' OR COLLECTION_ARRIVE_DATE IS NULL) \n");
        sb.append(" AND LEFT(CREATE_TIME,10) <=" + "'" + dateTime + "')\n");
        sb.append(" OR (ENDORSE_FLAG=1 AND ENDORSE_MODE=0 AND COLLECTION_ARRIVE_FLAG=1 \n ");
        sb.append(" AND COLLECTION_ARRIVE_DATE>= " + "'" + dateTime + "' \n ");
        sb.append(" AND LEFT(CREATE_TIME,10) <=" + "'" + dateTime + "') \n ");
        sb.append(" OR (ENDORSE_FLAG=1 AND ENDORSE_MODE IN ('1','2')  \n ");
        sb.append(" AND ENDORSE_DATE>=" + "'" + dateTime + "'\n ");
        sb.append(" AND LEFT(CREATE_TIME,10) <=" + "'" + dateTime + "'))\n ");
        List<JSONObject> jsonList = super.queryForJsonList(sb.toString());
        return jsonList;
    }

    public List<JSONObject> getBankDebitsByOutput(String dateTime, String companyId) {
        StringBuffer sb = new StringBuffer();
        sb.append(" SELECT ");
        sb.append(" t_cpt_bank_debits.ID as id, \n");
        sb.append(" t_cpt_bank_debits.MONEY as money, \n");
        sb.append(" t_cpt_bank_debits.OUTCOME_PROJECT AS outcomeProject, \n");
        sb.append(" t_cpt_bank_debits.DUE_DATE AS dueDate, \n");
        sb.append(" t_cpt_bank_debits.DRAWER_BANK AS drawerBank,\n");
        sb.append(" t_cpt_bank_debits.DEBITS_NO AS debitsNo \n");
        sb.append(" FROM `t_cpt_bank_debits`  \n");
        sb.append(" where 1=1 \n");
        sb.append(" AND FK_COMPANY_ID = " + "'" + companyId + "'");
        sb.append(" AND ((ENDORSE_FLAG=1 AND ENDORSE_MODE=0 AND COLLECTION_ARRIVE_FLAG=0 \n");
        sb.append(" AND DEL_FLAG=0 AND ENDORSE_DATE >=" + "'" + dateTime + "') \n");
        sb.append(" OR (ENDORSE_FLAG=1 AND ENDORSE_MODE=0 AND COLLECTION_ARRIVE_FLAG=1 \n");
        sb.append("  AND DEL_FLAG=0 AND (COLLECTION_ARRIVE_DATE>=" + "'" + dateTime + "' AND COLLECTION_ARRIVE_DATE IS NULL))\n");
        sb.append(" OR (ENDORSE_FLAG=1 AND ENDORSE_MODE IN ('1','2')  \n ");
        sb.append(" AND DEL_FLAG=0 AND ENDORSE_DATE=" + "'" + dateTime + "' \n ");
        sb.append(" AND LEFT(CREATE_TIME,10) <=" + "'" + dateTime + "')) \n ");
        List<JSONObject> jsonList = super.queryForJsonList(sb.toString());
        return jsonList;
    }


}

